package logic

import (
	"context"
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/tal-tech/go-zero/core/logx"
	"shape-erver/internal/svc"
	"shape-erver/internal/types"
	"shape-erver/utils"
	"strings"
)

type GetTaskResultLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetTaskResultLogic(ctx context.Context, svcCtx *svc.ServiceContext) GetTaskResultLogic {
	return GetTaskResultLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetTaskResultLogic) GetTaskResult(req types.GetTaskResultReq) (*types.GetTaskResultResp, error) {

	//status, _ := utils.GetTaskGuidFromRedis(req.TaskId)
	//if status != utils.SUCCESS {
	//	return &types.GetTaskResultResp{TaskId: req.TaskId, Message: "not success"}, nil
	//}

	task_id := req.TaskId
	dirpath := g.Cfg().GetString("dir_path")
	url := g.Cfg().GetString("dir_url")

	listFilePaths, err := utils.ListFileALLPath(dirpath + "/" + task_id)
	if err != nil {
		return nil, err
	}
	if len(listFilePaths) == 0 {
		return &types.GetTaskResultResp{TaskId: req.TaskId, Message: "no files"}, nil
	}
	fmt.Println(listFilePaths)

	//rert_file_task_id := "/ret_file/" + task_id

	var ret_files []string

	for _, filePath := range listFilePaths {

		if strings.Contains(filePath, "gif") || strings.Contains(filePath, "zip") {
			filePath = strings.Replace(filePath, dirpath, url+"/ret_file/", 1)

			ret_files = append(ret_files, filePath)
		}
	}

	return &types.GetTaskResultResp{TaskId: req.TaskId, Files: ret_files}, nil
}
